package com.my.mymafengwo.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smallstrong.mafengwopro.R;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.my.mymafengwo.base.BaseActivity;
import com.my.mymafengwo.bean.MddImg;
import com.my.mymafengwo.utils.ConstantUtils;
import com.my.mymafengwo.utils.UrlUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * ***********************************************
 * author:wgh
 * time:2015/12/8 12:05
 * copyright:
 * name:
 * step:
 * ***********************************************
 */
public class MddActivity extends BaseActivity implements View.OnSystemUiVisibilityChangeListener {

    private String city;
    private String url;
    private MddImg mddImg;
    private ImageView ivBack;
    private TextView tvCity, tvParent, tvNum;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private Toolbar toolBar;
    private BitmapUtils bitmapUtils;
    private ActionBar actionBar;


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_mdd, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_search:
                break;
            case R.id.menu_collect:
                break;
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void loadXml() {
        setContentView(R.layout.activity_mdd);
    }

    @Override
    protected void getIntentData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        city = intent.getStringExtra("city");
    }

    @Override
    protected void initViews() {
        collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        ivBack = (ImageView) findViewById(R.id.iv_backdrop);
        tvCity = (TextView) findViewById(R.id.tv_city);
        tvParent = (TextView) findViewById(R.id.tv_parent);
        tvNum = (TextView) findViewById(R.id.tv_num);
        toolBar = (Toolbar) findViewById(R.id.toolbar);
    }

    @Override
    protected void registerListener() {
    }

    @Override
    protected void initData() {
        bitmapUtils = new BitmapUtils(this);
        bitmapUtils.configDiskCacheEnabled(true);
        bitmapUtils.configMemoryCacheEnabled(false);
        bitmapUtils.configDefaultCacheExpiry(ConstantUtils.CACHEEXPIRY);
        setSupportActionBar(toolBar);
        actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.mipmap.icon_back_orange);
        actionBar.setDisplayHomeAsUpEnabled(true);
        collapsingToolbarLayout.setTitle(city);
        toolBar.setTitle(city);
        //收起时的背景颜色
        collapsingToolbarLayout.setContentScrimColor(getResources().getColor(R.color.floralwhite));
        //收起时title的颜色
        collapsingToolbarLayout.setCollapsedTitleTextColor(getResources().getColor(R.color.black));
//        collapsingToolbarLayout.setContentScrimResource(getResources().getLayout(R.layout.topbar_orange));
        //设置完全展开时title的颜色
        collapsingToolbarLayout.setExpandedTitleColor(Color.parseColor("#00000000"));
        downloadData();
    }

    /**
     * 下载数据并解析
     */
    private void downloadData() {
        switch (city) {
            case "上海":
                url = UrlUtils.MDD_SH;
                break;
            case "北京":
                url = UrlUtils.MDD_BJ;
                break;
            case "杭州":
                url = UrlUtils.MDD_HZ;
                break;
            case "宁波":
                url = UrlUtils.MDD_NB;
                break;
        }

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                if (result != null) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        JSONObject data = jsonObject.getJSONObject("data");
                        String mdd = data.getString("mdd");
                        mddImg = new Gson().fromJson(mdd, MddImg.class);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                if (mddImg != null) {
                    loadData();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    /**
     * 将数据加载到布局中
     */
    private void loadData() {
        if (ivBack == null) {
            System.out.println("null");
            return;
        }
        bitmapUtils.display(ivBack, mddImg.getHeader_img(), new BitmapLoadCallBack<ImageView>() {
            @Override
            public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                imageView.setImageBitmap(bitmap);
            }

            @Override
            public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {

            }
        });

        tvCity.setText(mddImg.getName());
        tvParent.setText(mddImg.getParent().getName());
        tvNum.setText(mddImg.getNum_album() + "张照片");
    }

    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        System.out.println("onSystemUiVisibilityChange");
    }
}
